package com.hyzx.qbasic.admin.controller.process;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.po.ProcessCategoryPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.IProcessCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 黄江华 on 2018/11/21
 */


@Api(tags = "流程分类", description = "ProcessCategoryController")
@Controller
@RequestMapping("/process/category")
public class ProcessCategoryController extends BaseController {

    @Autowired
    private IProcessCategoryService categoryService;

    @ApiIgnore
    @SysLog("流程分类首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.PROCESS_CATEGORY_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.PROCESS_CATEGORY_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.PROCESS_CATEGORY_DELETE));
        return "process/category/list";
    }

    @ApiOperation("获取流程分类列表")
    @GetMapping("/getProcessCategoryList")
    @ResponseBody
    public BaseResModel<PageInfo<ProcessCategoryPO>> getProcessCategoryList(@Valid GetProcessCategoryListReq req, BindingResult result) {
        return BaseResUtils.success(categoryService.getPagedProcessCategoryList(req));
    }

    @ApiIgnore
    @SysLog("新增流程分类页")
    @GetMapping("/add")
    public String add() {
        return "process/category/add";
    }

    @ApiOperation("新增流程分类表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddProcessCategoryReq req, BindingResult result) {
        BaseOperationRes res = categoryService.addProcessCategory(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("删除流程分类")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteProcessCategoryReq req, BindingResult result) {
        BaseOperationRes res = categoryService.deleteProcessCategory(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("编辑流程分类页")
    @GetMapping("/edit")
    public String edit() {
        return "process/category/edit";
    }

    @ApiOperation("编辑流程分类表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditProcessCategoryReq req, BindingResult result) {
        BaseOperationRes res = categoryService.editProcessCategory(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取流程分类信息")
    @GetMapping("/getProcessCategoryInfo")
    @ResponseBody
    public BaseResModel<ProcessCategoryPO> getProcessCategoryInfo(@Valid GetProcessCategoryInfoReq req, BindingResult result) {
        return BaseResUtils.success(categoryService.getProcessCategoryById(req.getProcessCategoryId()));
    }

    @SysLog("获取全部流程分类列表")
    @GetMapping("/getAllProcessCategoryList")
    @ResponseBody
    public BaseResModel<List<ProcessCategoryPO>> getAllProcessCategoryList() {
        return BaseResUtils.success(categoryService.getAllProcessCategoryList());
    }
}
